package org.jeecg.flowable.controller.definition;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.flowable.entity.definition.BpmForm;
import org.jeecg.flowable.service.definition.BpmFormService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;




@RestController
@RequestMapping("/bpm/form")
@Validated
public class BpmFormController {

    @Resource
    private BpmFormService formService;

    @PostMapping("/create")
    @AutoLog(value = "创建动态表单")
    @ApiOperation(value="创建动态表单", notes="创建动态表单")
    public Result<?> createForm(@Valid @RequestBody BpmForm bpmForm) {
        formService.save(bpmForm);
        return Result.ok(bpmForm.getId());
    }

    @PutMapping("/update")
    @AutoLog(value = "更新动态表单")
    @ApiOperation(value="更新动态表单", notes="更新动态表单")
    public Result<?> updateForm(@Valid @RequestBody BpmForm bpmForm) {
        formService.updateById(bpmForm);
        return Result.ok(true);
    }

    @DeleteMapping("/delete")
    @AutoLog(value = "删除动态表单")
    @ApiOperation(value="删除动态表单", notes="删除动态表单")
    public Result<?> deleteForm(@RequestParam("id") Long id) {
        formService.removeById(id);
        return Result.ok(true);
    }

    @GetMapping("/get")
    @AutoLog(value = "获得动态表单")
    @ApiOperation(value="获得动态表单", notes="获得动态表单")
    public Result<?> getForm(@RequestParam("id") Long id) {
        BpmForm form = formService.getById(id);
        return Result.ok(form);
    }

    @GetMapping("/list-all-simple")
    @AutoLog(value = "获得动态表单的精简列表")
    @ApiOperation(value="获得动态表单的精简列表", notes="用于表单下拉框")
    public Result<?> getSimpleForms() {
        List<BpmForm> list = formService.list();
        return Result.ok(list);
    }

    @GetMapping("/page")
    @AutoLog(value = "获得动态表单分页")
    @ApiOperation(value="获得动态表单分页", notes="获得动态表单分页")
    public Result<?> queryPageList(BpmForm bpmForm,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BpmForm> queryWrapper = QueryGenerator.initQueryWrapper(bpmForm, req.getParameterMap());
        Page<BpmForm> page = new Page<BpmForm>(pageNo, pageSize);
        IPage<BpmForm> pageList = formService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

}
